package com.ptswitch.material.pages.contract;

import java.util.Date;
import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.AppRuntimeException;
import org.hxzon.util.DateFormatUtil;
import org.hxzon.util.EMailUtil;
import org.hxzon.util.EMailUtil.EMail;

import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.model.contract.ContractItem;
import com.ptswitch.material.pages.base.MaterialBasePage;
import com.ptswitch.material.service.ContractItemService;
import com.ptswitch.material.service.ContractService;
import com.ptswitch.material.service.ErrorInputCacheService;
import com.ptswitch.material.util.MaterialUtil;

public class ReceiveItems extends MaterialBasePage {
    @Inject
    private ContractService contractSer;
    @Inject
    private ContractItemService contractItemSer;
    @Inject
    private ErrorInputCacheService errorInputCacheSer;
    @Property
    private String contractId;
    private Contract contract;
    @Property
    private List<ContractItem> list;
    @Property
    private String receivingRemark;
    //
    @InjectPage
    private ViewContract viewContract;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(String id) {
        this.contractId = id;
        contract = contractSer.findById(id);
        checkUserIsEqual(contract.getCreater());
        list = errorInputCacheSer.getReceiveItems(id, getVisitor());
        if (list == null) {
            list = contractItemSer.findByReceiving(id, false);
        }
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return contractId;
    }

    @OnEvent(value = EventConstants.SUCCESS, component = "receiving")
    Object update() {
        checkUserIsEqual(contract.getCreater());
        //just check
        for (ContractItem item : list) {
            if (item.getNumReceiving() + item.getNumCurrentReceive() > item.getQuantities()) {
                errorInputCacheSer.cacheReceiveItems(contractId, getVisitor(), list);
                throw AppRuntimeException.error(item.getCode() + "收货数大于购买数量");
            }
        }
        //update
        for (ContractItem item : list) {
            item.setNumReceiving(item.getNumCurrentReceive() + item.getNumReceiving());//update
            item.setReceivingDate(DateFormatUtil.formatToUniteTime(new Date()));
            item.setReceivingRemark(receivingRemark);
            item.computePrice();//update receiving price
        }
        contractItemSer.saveAll(list);
        errorInputCacheSer.removeReceiveItems(contractId, getVisitor());
        // update contract receiving ok
        Contract contract = contractSer.findById(contractId, ContractService.Init_Items);
        contract.updateForItem();
        contractSer.save(contract);
        //send email
        EMail email = getMaterialUtil().newEMail();
        email.addTo(contract.getProduction().getEmail());
        email.setSubject(contract.getCreater().getRealname() + "收到新货，请验收");
        email.addHtml(MaterialUtil.linkForViewContract(getWebUrl(), contract));
        email.addHtml(MaterialUtil.buildMessageForReceivingContractItem(getWebUrl(), contract, list));
        EMailUtil.send(email);
        return viewContract.initialize(contractId);
    }

}
